package com.retired_serviceman_management.controller;

import com.retired_serviceman_management.entity.EnterpriseInfo;
import com.retired_serviceman_management.entity.VocationalInfo;
import com.retired_serviceman_management.service.Enterprise_Info_Service;
import com.retired_serviceman_management.service.VocationalInfoService;
import com.retired_serviceman_management.util.Result;
import com.retired_serviceman_management.util.Soldier_util;
import com.retired_serviceman_management.util.Update_password_util;
import org.apache.commons.io.FilenameUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("Enterprise_info")
public class Enterprise_Info_Controller {

    @Resource
    private Enterprise_Info_Service enterprise_info_service;

    @Resource
    private VocationalInfoService vocationalInfoService;


    /*
    企业用户登录
     */
    @ResponseBody
    @RequestMapping(value = "login", method = RequestMethod.POST)

    public Result admin_login(@RequestBody EnterpriseInfo enterpriseInfo1, HttpServletRequest request) {
        System.out.println("enterpriseInfo1 = " + enterpriseInfo1);
//        Integer checkAccount = enterprise_info_service.CheckAccount(soldier_util.getComAccount(),soldier_util.getComPassword());
        Integer checkAccount = enterprise_info_service.CheckAccount(enterpriseInfo1.getComAccount(), enterpriseInfo1.getComPassword());
        System.out.println("checkAccount = " + checkAccount);
        if (checkAccount == 0) {//账号密码都正确,返回到显示企业信息页面，并且查询
            return new Result("0000", "欢迎登录", enterpriseInfo1.getComAccount());
        } else if (checkAccount == 1) {//账号不存在
            return new Result("0001", "账号不存在，请重新输入！");
//                request.setAttribute("flag1", "账号不存在!!!");
//                return "/jsp/Soldier_login.jsp";
        } else {//账号存在，密码错误
            return new Result("0002", "密码错误，请重新输入！");
//                request.setAttribute("flag2", "密码错误!!!");
//                return "/jsp/Soldier_login.jsp";
        }
    }

    /**
     * 企业用户查看企业信息
     */
    @RequestMapping(value = "enterprise_view/{comAccount}", method = RequestMethod.GET)
    public String enterprise_view(@PathVariable("comAccount") String comAccount, HttpServletRequest request) {
        EnterpriseInfo enterpriseInfo = enterprise_info_service.queryBycomAccount(comAccount);
        System.out.println("enterpriseInfo = " + enterpriseInfo);
        request.setAttribute("enterpriseInfo", enterpriseInfo);
        return "/jsp/Enterprise_info.jsp";
    }

    /**
     * 企业用户通过账号注册
     */
    @ResponseBody
    @RequestMapping(value = "register", method = RequestMethod.POST)
    public Result register(@RequestBody Soldier_util soldier_util) {
        System.out.println(soldier_util.getSoldierName() + "," + soldier_util.getMobile() + "," + soldier_util.getPassword() + "," + soldier_util.getPassword1());
        Integer integer = enterprise_info_service.register(soldier_util.getSoldierName(), soldier_util.getMobile(), soldier_util.getPassword(), soldier_util.getPassword1());
        System.out.println("integer = " + integer);
        if (integer == 1) {
            return new Result("1011", "注册成功，请登录");
        } else if (integer == 2) {
            return new Result("1012", "账号已存在，请重新输入");
        } else if (integer == 3) {
            return new Result("1014", "两次输入的密码不同，请重新输入");
        }
        return new Result("1010", "注册失败，请重试");
    }

    /**
     * 企业用户注销账号
     */
    @ResponseBody
    @RequestMapping(value = "Cancle_Enterprise", method = RequestMethod.GET)
    public Result Cancle_Enterprise(@RequestParam("comId") Integer comId) {
        Integer i = enterprise_info_service.Delete_Enterprise(comId);
        System.out.println("i = " + i);
        if (i == 0) {
            return new Result("0110", "注销失败");
        } else return new Result("0111", "注销成功");
    }

    /**
     * 企业用户修改密码
     */
    @ResponseBody
    @RequestMapping(value = "Update_password", method = RequestMethod.POST)
    public Result Update_password(@RequestBody Update_password_util soldier_util) {
        Integer i = enterprise_info_service.Update_password(soldier_util.getMobile(), soldier_util.getOldPassword(), soldier_util.getNewPassword(), soldier_util.getNewPasswordq());
        System.out.println("i = " + i);
        if (i == 1) {
            return new Result("1001", "修改成功");
        } else if (i == 0) {
            return new Result("1000", "修改失败");
        } else if (i == 2) {
            return new Result("1002", "原密码输入错误,请重新输入");
        } else if (i == 3) {
            return new Result("1003", "两次输入的密码不一致，请重新输入");
        }
        return new Result("4444", "网络错误，请重试");
    }

    /**
     * 查看职业信息
     */
    @RequestMapping(value = "View_vocation/{comAccount}", method = RequestMethod.GET)
    public String View_vocation(@PathVariable("comAccount") String comAccount, HttpServletRequest request) {
        System.out.println("comAccount = " + comAccount);
        VocationalInfo vocationalInfos = vocationalInfoService.queryByAccount(comAccount);
        System.out.println("vocationalInfos = " + vocationalInfos);
        request.setAttribute("vocationalInfos", vocationalInfos);
        return "/jsp/enterprise_vocation.jsp";
    }


    /**
     *修改职业信息
     */
    @ResponseBody
    @RequestMapping(value = "update_vocation",method = RequestMethod.POST)
    public Result update_vocation(String comAccount,String infoContent, @RequestParam(value="imgs",required = false) MultipartFile[] files) throws IOException{
        Integer integer = vocationalInfoService.update_vocation(comAccount, infoContent, files);
        if (integer==0) return new Result("0000","修改失败");
        else if(integer==1) return new Result("0001","修改成功",comAccount);
        return null;
    }

    /**
     * 重新提交职业信息
     */
    @ResponseBody
    @RequestMapping(value = "Submit_vocation", method = RequestMethod.GET)
    public Result Submit_vocation(@RequestParam("infoId") String infoId) {
        int infoId1 = Integer.parseInt(infoId);
        System.out.println("infoId = " + infoId);
        Integer integer = vocationalInfoService.Submit_vocation(infoId1);
        System.out.println("integer = " + integer);
        if (integer == 0) return new Result("0000", "提交审核失败");
        else if (integer == 1) return new Result("0001", "提交审核成功");
        else if (integer == 2) return new Result("0002", "审核已经通过，无需再次提交审核");
        else if (integer == 3) return new Result("0003", "当前还为未审核，请耐心等待");
        return null;
    }

    /**
     * 新建职业信息
     */
    @ResponseBody
    @RequestMapping(value = "Create_vocation", method = RequestMethod.POST)
    public Result Create_vocation(String comAccount, String infoContent, @RequestParam(value="imgs",required = false) MultipartFile[] files, HttpServletRequest request) throws IOException {
        int i = vocationalInfoService.insert1(comAccount, infoContent, files);
        if (i == 1) return new Result("0101", "提交成功");
        else if (i == 0) return new Result("0100", "提交失败");
        else if (i == 2) return new Result("0102", "该账号已经存在职业信息，请勿重复添加!");
        return null;
    }
}
